package com.example.demo.controller;


import com.example.demo.entity.BaseResponse;
import com.example.demo.entity.SysUser;
import com.example.demo.service.SysUserService;
import com.example.demo.service.UpdateLogService;
import com.example.demo.tool.DBConnection;
import com.example.demo.util.LocalCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;


import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping("/dlog")
@CrossOrigin
@RestController
public class LogController {

    @Autowired
    private UpdateLogService updateLogService;

    @Autowired
    private SysUserService sysUserService;

    //查询日志
    @GetMapping(value = "/selectlog")
    public BaseResponse<List<Map<String,Object>>> selectlog() {

        Map<String, Object> modelMap = new HashMap<>();
        List<Map<String,Object>> result1= updateLogService.getUpdateLog();
        return  BaseResponse.success(result1);
    }

    //删除日志
    @GetMapping(value = "/deletelog")
    public BaseResponse<Map<String,Object>> deletelog(@RequestParam(value = "id") int id) {
        Map<String, Object> result = updateLogService.deletelog(id);
        if(!"success".equals(result.get("state"))){
            return BaseResponse.fail(result);
        }
        return  BaseResponse.success(result);
    }
    //修改日志
    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        sdf.setTimeZone(TimeZone.getTimeZone("Asia/Shanghai"));
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
    @PostMapping(value = "/updatelog")
    public BaseResponse<Map<String,Object>> updatelog(@RequestParam(value = "id") int id,
                                                      @RequestParam(value = "studentName") String studentName,
                                                            @RequestParam(value = "studentId") String studentId,
                                                            @RequestParam(value = "stuclass") String stuclass,
                                                            @RequestParam(value = "exerciseId") int exerciseId,
                                                            @RequestParam(value = "questionId") int questionId,
                                                            @RequestParam(value = "qtitle") String qtitle,
                                                            @RequestParam(value = "qcontent") String qcontent,
                                                            @RequestParam(value = "difficulty") String difficulty,
                                                            @RequestParam(value = "opid") String opid,
                                                            @RequestParam(value = "opname") String opname,
                                                            @RequestParam(value = "optime") Date optime) {



        Map<String,Object> result1= updateLogService.modifyupdatelog(id,studentName,studentId,stuclass, exerciseId,questionId, qtitle,qcontent,difficulty,opid,opname,optime);
        if(result1.get("state")!="success"){
            return  BaseResponse.fail(result1);
        }
        return BaseResponse.success(result1);

    }


}
